/**
 * @file
 * @author    Hui Zhou&Shasha Liu
 * @version 2.0
 *
 * @section DESCRIPTION
 *
 *
 *
 *
 *
 *
 *
 */
package com.gph.etrip.timmy.test;

import java.io.*;
import java.net.*;
import java.util.ArrayList;

import org.xml.sax.Attributes;

import android.net.Uri;
import android.sax.*;
import android.util.Xml;

public class AndroidSaxXMLMarkerParser {
    public AndroidSaxXMLMarkerParser() {	}
    /**
     * Retrieve Marker objects from a stream containing an XML document.
     * @param inputStream The XML document.
     * @return Returns ArrayList of Marker objects retrieved from the stream. The array list will be empty on error, or if no markers were found.
     */
    public ArrayList<Marker> parse(InputStream inputStream) {
    	final InputStream is = inputStream;
        //final Marker marker = new Marker();
        RootElement root = new RootElement("markers");
        final ArrayList<Marker> markers = new ArrayList<Marker>();
        Element markerElement = root.getChild("marker");
        
        markerElement.setStartElementListener(new StartElementListener() {

			public void start(Attributes attributes) {
				String type = attributes.getValue("type");
				Integer id = Integer.parseInt(attributes.getValue("id"));
				String label = attributes.getValue("label");
				//Uri icon = Uri.parse(attributes.getValue("icon"));
				String icon = attributes.getValue("icon");
				
				Marker marker = new Marker(id, label, type, icon);
				markers.add(marker);
			}
        });
        
        try {
            //Xml.parse(this.getInputStream(), Xml.Encoding.UTF_8, root.getContentHandler());
        	Xml.parse(is, Xml.Encoding.UTF_8, root.getContentHandler());
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return markers;
    }
    public ArrayList<Marker> parse(InputStream inputStream ,final String category) {
    	final InputStream is = inputStream;
        //final Marker marker = new Marker();
        RootElement root = new RootElement("markers");
        final ArrayList<Marker> markers = new ArrayList<Marker>();
        Element markerElement = root.getChild("marker");
        
        markerElement.setStartElementListener(new StartElementListener() {

			public void start(Attributes attributes) {
				String type = attributes.getValue("type");
				Integer id = Integer.parseInt(attributes.getValue("id"));
				String label = attributes.getValue("label");
				//Uri icon = Uri.parse(attributes.getValue("icon"));
				String icon = attributes.getValue("icon");
				if(type.compareTo(category) == 0) {
					
					Marker marker = new Marker(id, label, type, icon);
					markers.add(marker);
			
				}

			}
        });
        
        try {
            //Xml.parse(this.getInputStream(), Xml.Encoding.UTF_8, root.getContentHandler());
        	Xml.parse(is, Xml.Encoding.UTF_8, root.getContentHandler());
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return markers;
    }
}
